﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTO;
using Business;

namespace Airplane
{
    public partial class ThongTinTaiKhoan : System.Web.UI.Page
    {
        private KhachHang _khachhang = new KhachHang();
        private XuLyCapNhatKhachHang capnhat = new XuLyCapNhatKhachHang();

        public KhachHang KhachHang
        {
            get { return _khachhang; }
            set { _khachhang = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((string)Session["TrangThaiDangNhap"] == "DaDangNhap")
                {
                    PanelDoiMatKhau.Visible = false;
                    txtHoTen.Text = Session["HoTen"].ToString();
                    txtTenTaiKhoan.Text = Session["TenTaiKhoan"].ToString();
                    if (Session["GioiTinh"].ToString() == "Nu")
                        rdbNu.Checked = true;
                    else
                        rdbNam.Checked = true;
                    txtDiaChi.Text = Session["DiaChi"].ToString();
                    txtNgaySinh.Text = Convert.ToDateTime(Session["NgaySinh"].ToString()).ToShortDateString();
                    txtemail.Text = Session["Email"].ToString();
                    txtDienThoai.Text = Session["DienThoai"].ToString();
                    txtSoGiayToTuyThan.Text = Session["SoGiayToTuyThan"].ToString();
                    if (Session["LoaiGiayToTuyThan"].ToString() == "HoChieu")
                        ddlGiayTo.SelectedValue = "HoChieu";
                    else
                        if (Session["LoaiGiayToTuyThan"].ToString() == "GiayKhaiSinh")
                            ddlGiayTo.SelectedValue = "GiayKhaiSinh";
                        else ddlGiayTo.SelectedValue = "CMND";                   
                    txtNgayCap.Text = Convert.ToDateTime(Session["NgayCap"].ToString()).ToShortDateString();
                    txtNuocPhatHanh.Text = Session["NuocPhatHanh"].ToString();
                    txtQuocTich.Text = Session["QuocTich"].ToString();
                    txtEmailPhucHoi.Text = Session["EmailPhucHoi"].ToString();
                    txtCredit.Text = Session["SoCreditCard"].ToString();
                    txtLoaiCredit.Text = Session["LoaiCreditCard"].ToString();
                    txtNgayHetHan.Text = Convert.ToDateTime(Session["NgayHetHanCredit"].ToString()).ToShortDateString();
                    //txtMatKhau.Text = Session["MatKhau"].ToString();
                    //txtNhacLai.Text = Session["MatKhau"].ToString();
                   
                    
                }
                else
                {
                    lbThongTin.Text = "Bạn chưa đăng nhập";

                    pnCapNhat.Visible = false;
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            pnCapNhat.Visible = true;
            PanelDoiMatKhau.Visible = false;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            PanelDoiMatKhau.Visible = true;
            pnCapNhat.Visible = false;
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Session["TrangThaiDangNhap"] = "ChuaDangNhap";
            Response.Redirect("TrangChu.aspx");
        }
        public void CapNhatThanhVien()
        {
            KhachHang.HoTen = txtHoTen.Text;
            if (rdbNu.Checked)
                KhachHang.GioiTinh = "Nu";
            else
            {
                KhachHang.GioiTinh = "Nam";
            }
            KhachHang.DiaChi = txtDiaChi.Text;
            KhachHang.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
            KhachHang.Email = txtemail.Text;
            KhachHang.DienThoai = txtDienThoai.Text;
            KhachHang.SoGiayToTuyThan = txtSoGiayToTuyThan.Text;
            KhachHang.LoaiGiayToTuyThan = ddlGiayTo.SelectedValue;
            KhachHang.NgayCap = Convert.ToDateTime(txtNgayCap.Text);
            KhachHang.QuocTich = txtQuocTich.Text;
            KhachHang.NuocPhatHanh = txtNuocPhatHanh.Text;
            KhachHang.TenTaiKhoan = txtTenTaiKhoan.Text;
            KhachHang.EmailPhucHoi = txtEmailPhucHoi.Text;
            KhachHang.SoCreditCard = txtCredit.Text;
            KhachHang.LoaiCreditCard = txtLoaiCredit.Text;
            KhachHang.NgayHetHanCredit = Convert.ToDateTime(txtNgayHetHan.Text);
            capnhat.khachhang = this.KhachHang;
            capnhat.ThucThi();
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {     
            try
            {
                CapNhatThanhVien();                
            }
            catch (Exception ex)
            {
                lbCapNhat.Text = ex.Message;
            }
        }

        protected void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text == (string)Session["MatKhau"]&&IsPostBack)
            {
                KhachHang.HoTen = txtHoTen.Text;
                if (rdbNu.Checked)
                    KhachHang.GioiTinh = "Nu";
                else
                {
                    KhachHang.GioiTinh = "Nam";
                }
                KhachHang.DiaChi = txtDiaChi.Text;
                KhachHang.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
                KhachHang.Email = txtemail.Text;
                KhachHang.DienThoai = txtDienThoai.Text;
                KhachHang.SoGiayToTuyThan = txtSoGiayToTuyThan.Text;
                KhachHang.LoaiGiayToTuyThan = ddlGiayTo.SelectedValue;
                KhachHang.NgayCap = Convert.ToDateTime(txtNgayCap.Text);
                KhachHang.QuocTich = txtQuocTich.Text;
                KhachHang.NuocPhatHanh = txtNuocPhatHanh.Text;
                KhachHang.TenTaiKhoan = txtTenTaiKhoan.Text;
                KhachHang.EmailPhucHoi = txtEmailPhucHoi.Text;
                KhachHang.SoCreditCard = txtCredit.Text;
                KhachHang.LoaiCreditCard = txtLoaiCredit.Text;
                KhachHang.NgayHetHanCredit = Convert.ToDateTime(txtNgayHetHan.Text);
                KhachHang.MatKhau = txtMatKhau.Text;
                capnhat.khachhang = KhachHang;
                capnhat.ThucThi();
                lbDoiMatKhau.Text = "Đổi mật khẩu thành công";
            }
        }
    }
}